package model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import pojo.RegisterBean;
import validate.LoginValidate;
import dao.MyDao;

@Controller
public class MyController {
	
	

	/**
	 * method to get Registr.html request
	 * 
	 * @param registerBean
	 * @return
	 */
	String logoutView = "login";
	String actionUrl = "#";
	String loginUser = "Log-in";
	String loginView = "login";

	static MyDao dao;
	static LoginValidate loginValidate;

	/**
	 * static block to getting bean object(LoginValidate) from
	 * applicationContext
	 */

	public static void setBean() {

		Resource r = new ClassPathResource("applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(r);

		// getting Mydao bean object for registering a user.. //dao = (MyDao)
		dao=(MyDao) factory.getBean("mydao");

		// getting LoginValidate bean object for validating any login..
		//loginValidate = (LoginValidate) factory.getBean("loginValidate");

	}

	@RequestMapping(value = "register.html", method = RequestMethod.GET)
	public String showRegister(@ModelAttribute("bean") RegisterBean registerBean) {
		System.out.println("MyController.showRegister()");

		return "register";
	}

	@RequestMapping(value = "register", method = RequestMethod.POST)
	public ModelAndView submitRegister(@Valid @ModelAttribute("bean") RegisterBean bean,BindingResult errors, Model model) {
		System.out.println("MyController.submitRegister()");

		System.out.println("Gender :" + bean.getGender());
		System.out.println("Birthdate :" + bean.getDate() + ":"
				+ bean.getMonth() + ":" + bean.getYear());
		System.out.println("Country :" + bean.getCountry());

		if (errors.hasErrors()) {
			System.out.println("error occured....");
			
			return new ModelAndView("register");
		}

		//getting bean...
		if(dao==null){
			setBean();	
		}
		
		
		dao.saveUser(bean);
		model.addAttribute("message",
				"successfully saved person" + bean.toString());
		return new ModelAndView("submitregister", "user", bean.getName());
	}

	/**
	 * Code to Handle parentslogin.html request
	 * 
	 */
	@RequestMapping(value = "parentslogin.html", method = RequestMethod.GET)
	public ModelAndView showParentLogin(Model model) {
		System.out.println("MyController.showParentLogin()");

		loginUser = "Parents-Log-in";
		actionUrl = "parentslogin.html";

		model.addAttribute("action", actionUrl);
		return new ModelAndView(loginView, "loginUser", loginUser);
	}

	@RequestMapping(value = "parentslogin.html", method = RequestMethod.POST)
	public ModelAndView submitParentLogin(HttpServletRequest req,
			HttpServletResponse res, Model model) {
		System.out.println("MyController.submitParentLogin()");
		model.addAttribute("loginUser", loginUser);

		/**
		 * Authenticating User's login
		 * 
		 */

		if ((!req.getParameter("email").equals(""))
				&& (!req.getParameter("pwd").equals(""))) {
			System.out.println("getting Resources...");

			/*Resource r = new ClassPathResource("applicationContext.xml");
			BeanFactory factory = new XmlBeanFactory(r);
			loginValidate = (LoginValidate) factory.getBean("loginValidate");*/
			if(loginValidate==null){
				setBean();
			}


			String name = loginValidate.authenticateParentsLogin(
					req.getParameter("email"), req.getParameter("pwd"));
			if (name != null) {
				
				  HttpSession session = req.getSession();
				  //session.setAttribute("welcomeUser", name);
				  session.setAttribute("session", session);
				  System.out.println("value of session :"+session);
				return new ModelAndView("submitparentslogin", "welcome",
						"Welcome :" + name);
			} else
				return new ModelAndView(loginView, "error",
						"Invalid Username or Password");
		}
		return new ModelAndView(loginView, "error",
				"UserName or Password can't be blank");
	}
	
	

	/**
	 * Admin Login Control..
	 * 
	 * 
	 */
	@RequestMapping(value = "adminlogin.html", method = RequestMethod.GET)
	public ModelAndView showAdminLogin(Model model) {
		System.out.println("MyController.showAdminLogin()");

		loginUser = "Admin-Log-in";
		actionUrl = "adminlogin.html";
		model.addAttribute("action", actionUrl);

		return new ModelAndView(loginView, "loginUser", loginUser);
	}

	/*@RequestMapping(value = "adminlogin.html", method = RequestMethod.POST)
	public ModelAndView submitAdminLogin(HttpServletRequest req,
			HttpServletResponse res, Model model) {
		System.out.println("MyController.submitAdminLogin()");
		System.out.println("ContextPath="+req.getContextPath());
		model.addAttribute("loginUser", loginUser);

		if ((!req.getParameter("email").equals(""))
				&& (!req.getParameter("pwd").equals(""))) {
			System.out.println("Email :"+req.getParameter("email"));
			if(loginValidate==null){
				
				setBean();
				System.out.println("bean sets successfully...");
			}
			String name = loginValidate.authenticateAdminLogin(
					req.getParameter("email"), req.getParameter("pwd"));
			if (name != null) {
				return new ModelAndView("submitAdminLogin", "welcome", "Welcome : Mr. " + name);
			}
			return new ModelAndView(loginView, "error",
					"Invalid User Name or Password");
		}
		return new ModelAndView(loginView, "error",
				"User Name or Password can't be blank");
	}
*/	
//**********************************after admin login process************************************************//	
	
	@RequestMapping(value="fullpage.html", method =RequestMethod.GET)
	public String showFullPage(){
		System.out.println("MyController.showFullPage()");
		
		
		dao.getList();
		return "page-full-width";
	}
	
	@RequestMapping(value="other.html", method =RequestMethod.GET)
	public String showOtherPage(){
		System.out.println("MyController.showOtherPage()");
		
		return "page-other";
		
	}
	
	@RequestMapping(value="dashboard.html", method=RequestMethod.GET)
	public String showDashboard(){
		System.out.println("MyController.showDashboard()");
		
		return "submitAdminLogin";
		
		
	}
	
	
	
	
	

	/**
	 * 
	 * Student Login...
	 * 
	 */
	@RequestMapping(value = "studentlogin.html", method = RequestMethod.GET)
	public ModelAndView showStudentLogin(Model model) {
		System.out.println("MyController.showStudentLogin()");

		loginUser = "Student-Log-in";
		actionUrl = "studentlogin.html";
		model.addAttribute("action", actionUrl);

		return new ModelAndView(loginView, "loginUser", loginUser);
	}

	@RequestMapping(value = "studentlogin.html", method = RequestMethod.POST)
	public ModelAndView submitStudentLogin(HttpServletRequest req,
			HttpServletResponse res, Model model) {
		System.out.println("MyController.submitStudentLogin()");

		model.addAttribute("loginUser", loginUser);
		return new ModelAndView("submitparentslogin", "welcome",
				"Welcome : U r Successfully logged in..");
	}

	/**
	 * 
	 * code to handle logout request..
	 */
	@RequestMapping(value = "logout.html", method = RequestMethod.GET)
	public ModelAndView doLogout(HttpServletRequest req,
			HttpServletResponse res, Model model) {
		System.out.println("MyController.doLogout()");

		model.addAttribute("action", actionUrl);
		model.addAttribute("loginUser", loginUser);
		model.addAttribute("backbutton", "disable");

		HttpSession session = req.getSession(false);
		if (session != null) {
			session.invalidate();
			return new ModelAndView(logoutView, "logout", "Successfully Logout");

		}
		else{
			System.out.println("login first...");
		}
		return new ModelAndView(logoutView, "error", "Login First");
	}

}
